
function openSettings() {
    const drawer = document.getElementById('settingsDrawer');
    // 先关闭终端抽屉（如果打开）
    document.getElementById('terminalDrawer').classList.add('translate-x-full');
    // 切换设置抽屉状态
    drawer.classList.toggle('-translate-x-full');
}

// 移除以下两个函数
// function saveSettings() {
//     const apiKey = document.getElementById('apiKeyInput').value;
//     closeSettings();
// }

// function closeSettings() {
//     document.getElementById('settingsDrawer').classList.add('-translate-x-full');
// }

// 点击抽屉外部关闭抽屉
document.addEventListener('click', (e) => {
    const settingsDrawer = document.getElementById('settingsDrawer');
    const terminalDrawer = document.getElementById('terminalDrawer');

    // 关闭设置抽屉（如果点击的不是设置按钮且不在抽屉内）
    if (!settingsDrawer.contains(e.target) && e.target.id !== 'settingsBtn') {
        settingsDrawer.classList.add('-translate-x-full');
    }

    // 关闭终端抽屉（如果点击的不是终端按钮且不在抽屉内）
    if (!terminalDrawer.contains(e.target) && (e.target.id !== 'newTerminalBtn' && e.target.id !== 'newTerminalBtn2')) {
        terminalDrawer.classList.add('translate-x-full');
    }
});

function addCustomParam() {
    const container = document.getElementById('customParamsContainer');
    const paramId = 'param_' + Date.now();

    const paramDiv = document.createElement('div');
    paramDiv.className = 'flex items-center gap-2';
    paramDiv.innerHTML = `
    <input type="text" placeholder="参数名" class="flex-1 bg-gray-700 rounded px-3 py-2">
    <input type="text" placeholder="参数值" class="flex-1 bg-gray-700 rounded px-3 py-2">
    <button onclick="removeParam(event,'${paramId}')" class="text-red-400 hover:text-red-300">
      <i class="fas fa-trash"></i>
    </button>
  `;
    paramDiv.id = paramId;

    container.appendChild(paramDiv);
}

function removeParam(event, id) {
    event.stopPropagation(); // 阻止事件冒泡
    document.getElementById(id).remove();
}

function toggleTerminalDrawer() {

    const drawer = document.getElementById('terminalDrawer');
    drawer.classList.toggle('translate-x-full');
    console.log(drawer.classList);
}

// 点击抽屉外部关闭抽屉
document.addEventListener('click', (e) => {
    const drawer = document.getElementById('terminalDrawer');
    if (!drawer.contains(e.target) && e.target.id !== 'newTerminalBtn'&& e.target.id !== 'newTerminalBtn2') {
        drawer.classList.add('translate-x-full');
    }
});

function openHistoryDrawer() {
    const drawer = document.getElementById('historyDrawer');
    // 先关闭其他抽屉
    document.getElementById('settingsDrawer').classList.add('-translate-x-full');
    document.getElementById('terminalDrawer').classList.add('translate-x-full');
    // 切换历史抽屉状态
    drawer.classList.toggle('-translate-x-full');
}

function closeHistoryDrawer() {
    document.getElementById('historyDrawer').classList.add('-translate-x-full');
}

// 更新点击外部关闭抽屉的逻辑
document.addEventListener('click', (e) => {
    const historyDrawer = document.getElementById('historyDrawer');
    const settingsDrawer = document.getElementById('settingsDrawer');
    const terminalDrawer = document.getElementById('terminalDrawer');

    // 关闭历史抽屉
    if (!historyDrawer.contains(e.target) && e.target.id !== 'historyBtn') {
        historyDrawer.classList.add('-translate-x-full');
    }

    // 关闭设置抽屉
    if (!settingsDrawer.contains(e.target) && e.target.id !== 'settingsBtn') {
        settingsDrawer.classList.add('-translate-x-full');
    }

    // 关闭终端抽屉
    if (!terminalDrawer.contains(e.target) && e.target.id !== 'newTerminalBtn' &&e.target.id !== 'newTerminalBtn2') {
        terminalDrawer.classList.add('translate-x-full');
    }
});

function deleteHistoryItem(event, button) {
    event.stopPropagation(); // 阻止事件冒泡，避免触发父元素的点击事件
    const item = button.closest('.p-3');
    item.remove();
    // 这里可以添加实际删除历史记录的逻辑
}
